package com.itheima.reggie.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.commom.CustomException;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.DishFlavor;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishFlavorMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Sea
 * @date 2023/3/5 16:37
 */
@Service
@Slf4j
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Override
    public Page<Dish> findByPage(Integer pageNum, Integer pageSize, String name) {


        // a. 分页+条件查询菜品  select * from dish where name like '%鱼%'  limit 0,10
        // 1.构建条件
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        qw.like(StrUtil.isNotEmpty(name), Dish::getName, name); // where name like '%鱼%'
        // 2.构建分页
        Page<Dish> page = new Page<>(pageNum, pageSize); // limit 0,10
        // 3.分页查询
        page = dishMapper.selectPage(page, qw);

        // 4.遍历菜品
        for (Dish dish : page.getRecords()) {
            // b. 查询分类名称  select * from category where id = 1397844263642378242
            Category category = categoryMapper.selectById(dish.getCategoryId());
            dish.setCategoryName(category.getName());

            // c.查询口味列表 select * from dish_flavor where dish_id = 1397850392090947585
            LambdaQueryWrapper<DishFlavor> qw2 = new LambdaQueryWrapper<>();
            qw2.eq(DishFlavor::getDishId, dish.getId());  // where dish_id = 1397850392090947585
            List<DishFlavor> flavorList = dishFlavorMapper.selectList(qw2);
            dish.setFlavors(flavorList);
        }

        // 5.返回分页
        return page;
    }

    @Override
    public void save(Dish dish) {
        // 1.保存菜品基本信息  insert into dish() values();
        log.info("菜品保存前ID：{}", dish.getId());
        dishMapper.insert(dish);
        log.info("菜品保存后ID：{}", dish.getId());

        // 2.取出前端提供口味列表并遍历
        List<DishFlavor> flavorList = dish.getFlavors();
        for (DishFlavor dishFlavor : flavorList) {
            // 2-1 关联菜品id
            dishFlavor.setDishId(dish.getId());
            // 2-2 保存数据库  insert into dish_flavor() values();
            dishFlavorMapper.insert(dishFlavor);

        }

    }

    @Override
    public Dish findById(Long dishId) {
        // 1.查询菜品基本信息   select * from dish where id = 1632199055591378946
        Dish dish = dishMapper.selectById(dishId);


        // 2.构建口味查询条件 select * from dish_flavor where dish_id = 1632199055591378946
        LambdaQueryWrapper<DishFlavor> qw = new LambdaQueryWrapper<>();
        qw.eq(DishFlavor::getDishId, dishId); //  where dish_id = 1632199055591378946

        // 3.查询口味列表
        List<DishFlavor> flavorList = dishFlavorMapper.selectList(qw);

        // 4.将口味封装到菜品
        dish.setFlavors(flavorList);

        // 5.返回菜品
        return dish;
    }

    @Override
    public void updateDish(Dish dish) {
        //1
        dishMapper.updateById(dish);
        //2
        LambdaQueryWrapper<DishFlavor> QW = new LambdaQueryWrapper<>();
        QW.eq(DishFlavor::getDishId, dish.getId());
        dishFlavorMapper.delete(QW);
        //3

        // 3.获取前端新提供的口味列表 遍历
        for (DishFlavor flavor : dish.getFlavors()) {
            // 3-1 口味关联菜品id
            flavor.setDishId(dish.getId());
            // 3-2 保存数据库
            dishFlavorMapper.insert(flavor);
        }

    }

    @Override
    public void updateStatus(Integer status, List<Long> ids) {
        if (CollUtil.isNotEmpty(ids)) {
            for (Long id : ids) {
                Dish dish = new Dish();
                dish.setId(id);
                dish.setStatus(status);
                dishMapper.updateById(dish);
            }
        }
    }

    @Override
    public void deleteDish(List<Long> ids) {
        LambdaQueryWrapper<Dish> dishQW = new LambdaQueryWrapper<>();
        dishQW.in(Dish::getId,ids);
        dishQW.eq(Dish::getStatus,1);
        Integer dishCount = dishMapper.selectCount(dishQW);
        if (dishCount>0){
            throw new CustomException("有起售菜品，无法删除");
        }
        dishMapper.deleteBatchIds(ids);
        LambdaQueryWrapper<DishFlavor> flavorQw = new LambdaQueryWrapper<>();
        flavorQw.in(DishFlavor::getDishId,ids);
        dishFlavorMapper.delete(flavorQw);
    }

    @Override
    public List<Dish> findListBySetmeal(Long categroyId, String name) {
        //构造条件
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StrUtil.isNotEmpty(name),Dish::getName, name);
        queryWrapper.eq(categroyId!=null,Dish::getCategoryId,categroyId);
        return dishMapper.selectList(queryWrapper);
    }

    @Override
    public List<Dish> list(Long categoryId, Integer status) {
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Dish::getCategoryId,categoryId);
        queryWrapper.eq(Dish::getStatus,status);
        List<Dish> dishList = dishMapper.selectList(queryWrapper);
        //遍历菜品
        dishList.forEach(dish -> {
            LambdaQueryWrapper<DishFlavor> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(DishFlavor::getDishId,dish.getId());
            List<DishFlavor> flavorList = dishFlavorMapper.selectList(queryWrapper1);
            dish.setFlavors(flavorList);
        });
        return dishList;
    }
}


